using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using System.Xml.Serialization;
using Jage.Utils;

namespace Jage
{
    /// <summary>
    /// Contains graphics resources that belong to a graphical theme
    /// </summary>
    public class Theme
        : SaveObject<Theme>
    {

        public Theme()
        {
            Terrain = new TerrainTheme();
            Foliage = new List<FoliageTheme>();
        }

        public void LoadResources(ContentManager contentManager)
        {
            Terrain.LoadResources(contentManager);
            foreach (var foliage in Foliage)
            {
                foliage.LoadResources(contentManager);
            }
        }

        static private Theme _current;

        static public void SetCurrentTheme(string filename)
        {
            _current = Theme.LoadFromFile(Constants.DataFolder + "\\" + filename);
        }

        static public Theme Current
        {
            get
            {
                return _current;
            }
        }

        [XmlAttribute()]
        public string Name { get; set; }

        public TerrainTheme Terrain { get; set; }

        public List<FoliageTheme> Foliage { get; set; }

    }

    public class TerrainTheme
    {

        public TerrainTheme()
        {
            Ground = new List<ThemeResource>();
            GroundVariations = new List<ThemeResource>();
        }

        public void LoadResources(ContentManager contentManager)
        {
            foreach (ThemeResource resource in Ground)
            {
                resource.Texture = contentManager.Load<Texture2D>("Terrain\\Ground\\" + resource.Filename);
            }
            foreach (ThemeResource resource in GroundVariations)
            {
                resource.Texture = contentManager.Load<Texture2D>("Terrain\\GroundVariations\\" + resource.Filename);
            }
        }

        public List<ThemeResource> Ground { get; set; }

        public List<ThemeResource> GroundVariations { get; set; }

    }

    public class FoliageTheme
    {

        public FoliageTheme()
        {
            Variations = new List<ThemeResource>();
        }

        public void LoadResources(ContentManager contentManager)
        {
            foreach (ThemeResource resource in Variations)
            {
                resource.Texture = contentManager.Load<Texture2D>(String.Format("Foliage\\{0}\\{1}", Name, resource.Filename));
            }
        }

        [XmlAttribute()]
        public string Name { get; set; }

        public List<ThemeResource> Variations { get; set; }

    }

    public class ThemeResource
    {

        public ThemeResource()
        {
        }

        public ThemeResource(string filename)
        {
            Filename = filename;
        }

        [XmlAttribute()]
        public string Filename;

        [XmlIgnore()]
        public Texture2D Texture;

    }

}
